#include "player.h"


//用户界面
int playerFace()
{
    while(1)
    {
        system("clear");
        printf("请选择需要的功能:\n");
        printf("1.查看个人信息\n");
        printf("2.修改密码\n");
        printf("3.账户充值\n");
        printf("4.下注\n");
        printf("5.查看历史记录\n");
        printf("6.注销\n");
        printf("0.返回登录界面\n");
        int iCho=9;
        if(1 != scanf("%d",&iCho))
        {
            chidaodi;
        }
        else
        {
            getchar();
        }
        if(iCho!=1&&iCho!=2&&iCho!=3&&iCho!=4&&iCho!=5&&iCho!=6&&iCho!=0)
        {
            printf("选择错误！两秒后请重新选择!\n");
            sleep(2);
            continue;
        }
        return iCho;
    }
}

//彩民的选择
void switchByPlayer(usrNode *node)
{
    int ret=playerFace();
    switch (ret)
    {
    case 1:
        playerDisplay(node);
        switchByPlayer(node);
        break;
    case 2:
        node=changePassword(PATH_NAME,node->usrName);
        switchByPlayer(node);
        break;
    case 3:
        node=becomeStronger(PATH_NAME,node->usrName);
        switchByPlayer(node);
        break;
    case 4:
        node=howToStronger(node);
        switchByPlayer(node);
        break;
    case 5:
        inquireHistory(node);
        switchByPlayer(node);
        break;
    case 6:
        rmUsrInfo(node);
        //free(node);
        logOrRegFace();
        break;
    case 0:
        free(node);
        loginSwitch();
        break;
    
    default:
        break;
    }
}

//查看个人信息
void playerDisplay(const usrNode *node)
{
    qingping;
    printf("信息如下：\n");
    printf("用户名:%s\n",node->usrName);
    printf("密码：%s\n",node->usrPwd);
    printf("余额：%.2lf\n",node->usrMoney);
    if(node->usrMoney < 1000)
    {
        printf("您的余额已不多，请及时充值！\n");
    }
    huichejixu();
}

//修改密码
usrNode *changePassword(const char *path,char *caUsrName)
{
    qingping;
    usrList *list=mkList();
    loadInfoFromFile(path,list);
    usrNode *node=findNodeByName(list,caUsrName);
    int flag=0;
    char newPwd[PWD_LEN]={'\0'};
    char newPwd2[PWD_LEN]={'\0'};
    while (flag<TIMES_PWD)
    {
        printf("请输入旧密码：\n");
        getPassword(newPwd);
        //scanf("%s",newPwd);
        if(strcmp(newPwd,node->usrPwd) == 0)
        {
            break;
        }
        else
        {
            printf("输入错误！\n");
            flag++;
        }
    }
    if(strcmp(newPwd,node->usrPwd) != 0)
    {
        printf("三次错误！\n");
        releaseUsrList(node,list);
        huichejixu();
        return node;
    }
    printf("请输入新密码：\n");
    getPassword(newPwd);
    flag=0;
    while (flag<TIMES_PWD)
    {
        printf("请再次输入新密码：\n");
        getPassword(newPwd2);
        if(strcmp(newPwd,newPwd2) == 0)
        {
            strcpy(node->usrPwd,newPwd);
            saveListToFile(path,list);
            printf("密码修改成功！\n");
            releaseUsrList(node,list);
            huichejixu();
            return node;
        }
        else
        {
            printf("输入错误！\n");
            flag++;
        }
    }
    if(strcmp(newPwd,newPwd2) != 0)
    {
        printf("三次错误！修改失败!\n");
        releaseUsrList(node,list);
        huichejixu();
        return node;
    }
    releaseUsrList(node,list);
    huichejixu();
    return node;   
}

//充值
usrNode *becomeStronger(const char *path,char *caUsrName)
{
    usrList *list=mkList();
    loadInfoFromFile(path,list);
    usrNode *node=findNodeByName(list,caUsrName);
    double yourMoney=0;
    while(1)
    {
        system("clear");
        printf("变强的第一步：\n");
        printf("请输入充值金额：\n");
        scanf("%lf",&yourMoney);
        chidaodi;
        if(yourMoney<=0 || yourMoney>=1000000)
        {
            printf("对不起！一次充值最多1000000，请理性氪金！\n");
            printf("一秒后请重新充值...\n");
            sleep(1);
            chidaodi;
            continue;
        }
        break;
    }
    node->usrMoney=node->usrMoney+yourMoney;
    saveListToFile(path,list);
    printf("恭喜你，你变的更强了！\n");
    releaseUsrList(node,list);
    huichejixu();
    return node;
}

//下注
usrNode * howToStronger(usrNode *usrInfoNode)
{
    qingping;
    usrList *usrInfoList=mkList();
    loadInfoFromFile(PATH_NAME,usrInfoList);
    usrInfoNode = findNodeByName(usrInfoList,usrInfoNode->usrName);
    lyList *issueList=mkLyList();
    loadLotteryInfoFromFile(LOTTERY_PATH,issueList);
    lyNode *node=findNotOpenLyNode(issueList);
    if(node == NULL)
    {
        printf("新一期彩票还未发布！请等管理员发布后再下注\n");
        releaseLyList(NULL,issueList);
        releaseUsrList(usrInfoNode,usrInfoList);
        free(node);
        huichejixu();
        return usrInfoNode;
    }
    buyLyList *boughtList=mkBuyLyList();
    loadBuyLotteryInfoFromFile(BUYLOTTERY_PATH,boughtList);
    printf("欢迎购买第%d期彩票！\n",node->issue);
    int i=0;
    buyLyNode *buyNode=mkBuyLyNode();
    for(i=0;i<LOTTERY_LEN;i++)
    {
        while(1)
        {
            printf("请输入第%d个号码:\n",i+1);
            scanf("%d",&(buyNode->lottery[i]));
            if(buyNode->lottery[i]<0 || buyNode->lottery[i]>9)
            {
                printf("请输入0-9！\n");
                sleep(1);
                continue;
            }
            break;
        }
    }
    chidaodi;
    while(1)
    {
        printf("请输入购买的注数(最多五注！）：\n");
        scanf("%d",&(buyNode->count));
        if(buyNode->count<=0 || buyNode->count > 5)
        {
            printf("您输入的注数有误！两秒后请重新输入!");
            sleep(2);
            continue;
        }
        chidaodi;
        break;
    }
    double res=usrInfoNode->usrMoney - (node->price*buyNode->count);
    if(res < 0)
    {
        printf("对不起余额不足！请先充值！\n");
        huichejixu();
        releaseBuyLyList(NULL,boughtList);
        releaseLyList(NULL,issueList);
        releaseUsrList(usrInfoNode,usrInfoList);
        free(buyNode);
        return usrInfoNode;
    } 
    strcpy(buyNode->openstatus,NOT_OPEN);
    strcpy(buyNode->player,usrInfoNode->usrName);
    buyNode->issue=node->issue;
    buyLotteryInsert(boughtList,buyNode);
    saveBuyLotteryListToFile(BUYLOTTERY_PATH,boughtList);

    usrInfoNode->usrMoney=res;
    saveListToFile(PATH_NAME,usrInfoList);

    node->sum_num=node->sum_num+buyNode->count;
    node->sum_mon=node->sum_num*node->price;
    saveLotteryListToFile(LOTTERY_PATH,issueList);
    printf("恭喜你！下注成功！\n");
    releaseBuyLyList(NULL,boughtList);
    releaseLyList(NULL,issueList);
    releaseUsrList(usrInfoNode,usrInfoList);
    free(buyNode);
    huichejixu();
    return usrInfoNode;
}

//查询历史购买记录
void inquireHistory(usrNode *usrInfoNode)
{
    buyLyList *boughtList=mkBuyLyList();
    loadBuyLotteryInfoFromFile(BUYLOTTERY_PATH,boughtList);
    buyLyNode *boughtNode=boughtList->pFirstNode;
    printf("期号\t彩票号\t注数\t开奖状态\t中奖金额\n");
    while(boughtNode != NULL)
    {
        if(strcmp(boughtNode->player,usrInfoNode->usrName) == 0)
        {
            int i=0;
            printf("%d\t",boughtNode->issue);
            for(i=0;i<LOTTERY_LEN;i++)
            {
                printf("%d",boughtNode->lottery[i]);
            }
            printf("\t%d\t%s\t\t%.2lf\n",boughtNode->count,boughtNode->openstatus, boughtNode->money);
        }
        boughtNode=boughtNode->pnext;
    }
    releaseBuyLyList(NULL,boughtList);
    huichejixu();
}

//注销
void rmUsrInfo(usrNode *usrInfoNode)
{
    usrList *usrInfoList=mkList();
    loadInfoFromFile(PATH_NAME,usrInfoList);
    usrInfoList=delUsrInfo(usrInfoNode,usrInfoList);
    saveListToFile(PATH_NAME,usrInfoList);
    printf("注销成功！你放弃了变强的机会！\n");
    releaseUsrList(NULL,usrInfoList);
    huichejixu();
}

//打印彩民购买彩票信息
void displayBuyNode(buyLyNode *boughtNode)
{
    //printf
}
